﻿/*

Copyright (c) 2009, Stephen Remde
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <organization> nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY STEPHEN REMDE ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL STEPHEN REMDE BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq;

namespace ConceptLink
{
    /// <summary>
    /// Retrieves and caches relatedness
    /// </summary>
    public class Relatedness : JSPTask
    {
        /// <summary>
        /// internal storage class
        /// </summary>
        private class _Store
        {
            public string a, b;
            public float r;

            public override bool Equals(object obj)
            {
                _Store o = obj as _Store;
                return a.Equals(o.a) && b.EndsWith(o.b);
            }
            public override int GetHashCode()
            {
                return a.GetHashCode() ^ b.GetHashCode();
            }
        }

        // hash bucket style storage        
        private const int bits = 0xfff;
        private static List<_Store>[] cache = new List<_Store>[bits + 1];

        static Relatedness()
        {
            for (int i = 0; i <= bits; i++)
                cache[i] = new List<_Store>();
        }

        public delegate void RelatednessCallback(string article1, string article2, float relatedness);
        public delegate void MassRelatednessCallback(string article1, string[] article2, float[] relatedness);

        /// <summary>
        /// trys to get a relatedness from the cache
        /// </summary>
        /// <param name="article1">first article name</param>
        /// <param name="article2">second article name</param>
        /// <param name="relatedness">returned relatedness if the value was in the cache</param>
        /// <returns>true if the value was in the cache</returns>
        public static bool TryGet(string article1, string article2, out float relatedness)
        {
            string articlea = article1.ToLower();

            if (articlea.Contains("(disambiguation)"))
            {
                relatedness = 0;
                return true;
            }

            string articleb = article2.ToLower();

            if (articleb.Contains("(disambiguation)"))
            {
                relatedness = 0;
                return true;
            }

            int a = articleb.CompareTo(articlea);

            if (a == 0)
            {
                relatedness = 1;
                return true;
            }
            else if (a < 0)
            {
                string tmp = articlea;
                articlea = articleb;
                articleb = tmp;
            }

            _Store s = new _Store() { a = articlea, b = articleb };

            var ca = cache[s.GetHashCode() & bits];

            lock (ca)
            {
                foreach (var entry in ca)
                    if (entry.a == articlea && entry.b == articleb)
                    {
                        relatedness = entry.r;
                        return true;
                    }
            }

            relatedness = 0;

            return false;
        }

        /// <summary>
        /// MassGet article relatedness
        /// </summary>
        /// <param name="article1">article to be compared to</param>
        /// <param name="article2">articles to compare to</param>
        /// <param name="callback">call back function with results</param>
        public static void MassGet(string article1, string[] article2, MassRelatednessCallback callback)
        {
            List<string> need = new List<string>();

            float[] ret = new float[article2.Length];
                        
            // do we actually need them?
            float f;
            foreach (string arti in article2)
            {
                if (TryGet(article1, arti, out f))
                {
                    ret[Array.IndexOf<string>(article2, arti)] = f;
                }
                else
                {
                    need.Add(arti);
                }
            }

            if (need.Count == 0)
            {
                // we have all the data
                callback(article1, article2, ret);
            }
            else
            {
                // we need to get some!
                int atonce = 40; // get 40 at once
                int blocks = (need.Count + atonce-1) / atonce;

                for (int i = 0; i < blocks; i++)
                {
                    GetXml(string.Format(masscompareurl, Uri.EscapeDataString(article1), Uri.EscapeDataString(string.Join("|", need.Skip(i*atonce).Take(atonce).ToArray()))),
                    delegate(XDocument x)
                    {
                        try
                        {
                            foreach (var node in x.Descendants("RelatednessResponse"))
                            {
                                try
                                {
                                    float rel = float.Parse(node.Attribute("relatedness").Value);
                                    string arti = node.Attribute("term2").Value;
                                    ret[Array.IndexOf<string>(article2, arti)] = rel;
                                    Add(article1, arti, rel);
                                }
                                catch
                                {

                                }
                            }
                        }
                        catch
                        {

                        }
                        blocks--;

                        if (blocks == 0)
                            callback(article1, article2, ret);
                    });
                }
            }
        }

        /// <summary>
        /// get the relatedness. if the value is not in the cache, 0 is returned
        /// </summary>
        /// <param name="article1">first article</param>
        /// <param name="article2">second article</param>
        /// <returns></returns>
        public static float Get(string article1, string article2)
        {
            float ret = 0;
            TryGet(article1, article2, out ret);
            return ret;
        }

        /// <summary>
        /// get the relatedness. if the value is not in the cache, the value is requested from the server
        /// </summary>
        /// <param name="article1">first article</param>
        /// <param name="article2">second article</param>
        /// <param name="callback">callback function</param>
        public static void Get(string article1, string article2, RelatednessCallback callback)
        {
            float f;

            // do we have it cached?
            if (TryGet(article1, article2, out f))
            {
                callback(article1, article2, f);
                return;
            }

            // go get it!
            GetXml(string.Format(compareurl, Uri.EscapeDataString(article1), Uri.EscapeDataString(article2)),
                delegate(XDocument x)
                {                    
                    try
                    {
                        var t = x.Descendants("RelatednessResponse").FirstOrDefault().Attribute("relatedness");

                        float rel = 0;

                        if (t != null)
                        {
                            rel = float.Parse(t.Value);

                            Add(article1, article2, rel);
                        }

                        callback(article1, article2, rel);
                    }
                    catch
                    {
                        callback(article1, article2, 0);
                    }
                });
        }

        /// <summary>
        /// adds a relatedness to the cache
        /// </summary>
        /// <param name="article1">a</param>
        /// <param name="article2">b</param>
        /// <param name="rel">rel(a,b)</param>
        internal static void Add(string article1, string article2, float rel)
        {
            string articlea = article1.ToLower();
            string articleb = article2.ToLower();

            int a = articleb.CompareTo(articlea);

            if (a < 0)
            {
                string tmp = articlea;
                articlea = articleb;
                articleb = tmp;
            }
            else if (a == 0)
            {
                return;
            }

            _Store s = new _Store() { a = articlea, b = articleb, r = rel };

            var ca = cache[s.GetHashCode() & bits];

            lock (ca)
            {
                ca.Add(s);
            }
        }
    }
}
